package com.servlet.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.entity.DetailProduct;
import com.entity.Order;
import com.entity.User;
import com.service.product.impl.OrderServiceImpl;
import com.servlet.BaseServlet;
import com.utils.EmptyUtils;
import com.utils.Pager;

@WebServlet("/AdminOrderServlet")
public class AdminOrderServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
		
	}
	
	public void index2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("123");
	}
	
	/**
	 * 订单列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取ID！
		User userId = (User)request.getSession().getAttribute("easybuyUserLogin");
		// 获取当前页数！
		String currentPageStr = request.getParameter("currentPage");
		// 获取模糊查询条件
		// 获取页大小！
		String pageSize = request.getParameter("pageSize");
		int rowPerPage = EmptyUtils.isEmpty(pageSize) ? 2 : Integer.parseInt(pageSize);
		int currentPage = EmptyUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr);
		int count = new OrderServiceImpl().finuserByIdOrder(userId.getId());	//总记录数
		Pager pager = new Pager(count, rowPerPage, currentPage);
		if (pager.getPageCount() < pager.getCurrentPage()) {
			pager.setCurrentPage(currentPage - 1);

		}
		List<Order> orderList = null;
		if(count != 0) {
			orderList = new OrderServiceImpl().finOrderList(userId.getId(),pager);
		}
		
		pager.setUrl("/AdminOrderServlet?action=index&userId=" + userId.getId());
		// 获取当前登录用户ID！
		// 调用三层！
		List<DetailProduct> listOrderDetail = new OrderServiceImpl().finOrderDetail();
		// 放置内置对象！
		// 放置对象！
		request.setAttribute("orderList", orderList);
		request.setAttribute("listOrderDetail", listOrderDetail);
		request.setAttribute("pager", pager);
		request.setAttribute("menu", 1);
		return "backend/order/orderList"; 	
	}
	
	/**
	 * 模糊查询
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String likeindex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取ID！
		User user = (User)request.getSession().getAttribute("easybuyUserLogin");
		System.out.println(user.getId());
		// 获取当前页数！
		String currentPageStr = request.getParameter("currentPage");
		// 获取模糊查询条件
		String namelike = EmptyUtils.isEmpty(request.getParameter("keyWord")) ? null:request.getParameter("keyWord");
		System.out.println(namelike);
		// 获取页大小！
		String pageSize = request.getParameter("pageSize");
		int rowPerPage = EmptyUtils.isEmpty(pageSize) ? 2 : Integer.parseInt(pageSize);
		int currentPage = EmptyUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr);
		int count = 0;
		if(namelike == null) {
			count = new OrderServiceImpl().finuserByIdOrder(user.getId());	//总记录数
		}else {
			count = new OrderServiceImpl().finOrderLikeCount(namelike,user.getId());	//总记录数
		}
		System.out.println(count);
		Pager pager = new Pager(count, rowPerPage, currentPage);
		if (pager.getPageCount() < pager.getCurrentPage()) {
			pager.setCurrentPage(currentPage - 1);

		}
		List<Order> orderList = null;
		if(count > 0) {
			orderList = new OrderServiceImpl().finOrderListLike(namelike,user.getId(),pager);
		}
		pager.setUrl("/AdminOrderServlet?action=index&userId=" + user.getId());
		// 获取当前登录用户ID！
		// 调用三层！
		List<DetailProduct> listOrderDetail = new OrderServiceImpl().finOrderDetail();
		// 放置内置对象！
		request.setAttribute("orderList", orderList);
		request.setAttribute("listOrderDetail", listOrderDetail);
		request.setAttribute("pager", pager);
		request.setAttribute("menu", 1);
		return "backend/order/orderList";
	}
	
	/**
	 * 查询订单明细！
	 * @param request
	 * @param response
	 * @return
	 */
	public String queryOrderDeatil(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 获取ID！
		String orderId = request.getParameter("id");
		// 访问三层！
		List<DetailProduct> listOrderDetail = new OrderServiceImpl().finOrderDetail();
		// 放置内置对象！
		request.setAttribute("orderId", Integer.parseInt(orderId));
		request.setAttribute("listOrderDetail", listOrderDetail);
		return "/backend/order/orderDetailList";
	}
	
	/**
	 * 全部订单！
	 */
	public String queryAllOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 获取当前页数！
		String currentPageStr = request.getParameter("currentPage");
		// 获取页大小
		String pageSize = request.getParameter("pageSize");
		int rowPerPage = EmptyUtils.isEmpty(pageSize) ? 2 : Integer.parseInt(pageSize);
		int currentPage = EmptyUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr);
		List<Order> orderList = new OrderServiceImpl().finOrderList(0,null);
		int total = new OrderServiceImpl().finOrderDetailCount();
		Pager pager = new Pager(total, rowPerPage, currentPage);
		pager.setUrl("/AdminOrderServlet?action=queryAllOrder");
		// 访问三层！
		List<DetailProduct> listOrderDetail = new OrderServiceImpl().finOrderDetail();
		// 放置对象！
		if((pager.getRowPerPage()*currentPage)>=total) {
			request.setAttribute("orderList", orderList.subList(pager.getCurrentPage(), orderList.size()));
		}else {
			request.setAttribute("orderList", orderList.subList(pager.getCurrentPage(), pager.getRowPerPage()*currentPage));
		}
		request.setAttribute("listOrderDetail", listOrderDetail);
		request.setAttribute("pager", pager);
		request.setAttribute("menu", 9);
		return "/backend/order/orderList";
	}

	@Override
	public Class<?> getServletClass() {
		// TODO Auto-generated method stub
		return AdminOrderServlet.class;
	}
	
	

}
